package com.shop.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import com.jf.core.AuthInterceptor;
import com.jf.core.BaseController;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.shop.bean.AddressBean;
import com.shop.bean.POrderBean;
import com.shop.bean.UserBean;
import com.shop.utils.StringUtils;

/**
 * 我的订单 控制器
 * @author bsh_2012@163.com
 *
 */
public class COrderController extends BaseController {
	    //等待用户收货的订单列表
	    @Before(AuthInterceptor.class)
		public void ddsh(){
			String ucode = ((UserBean)getSessionAttr("user")).getStr("ucode");
			String select = "select tblp.phyprice,tblop.psum,tblpo.ocode,tblpo.otime,tblpo.oprice,tblpo.odeal,tblpo.odeal1,tblpo.odeal2,tblpo.odeal3,tblpo.odeal4,tblp.pname,tblp.pphoto,tblp.pcode,tbladdr.name,tblzf.zname";
			String sqlExceptSelect = "from tblshrmess tblsm inner join tblporder tblpo on tblsm.ocode= tblpo.ocode and tblsm.ucode=? and tblpo.odeal3='1' and tblpo.odeal4='0' and tblpo.ostate=1 "+
			                         "inner join tblzhifu tblzf on tblzf.zcode=tblsm.zffs "+
					                 "inner join tbladdress tbladdr on tbladdr.adcode=tblsm.adcode "+
			                         "inner join tblorderproduct tblop on tblop.ocode=tblpo.ocode "+
			                         "inner join tblproduct tblp on tblp.pcode=tblop.pcode order by tblpo.ocode,tblpo.otime desc";
			Integer page = getParaToInt("page");
			if(page == null || "".equals(page)){
				page= 1;
			}
			Page<Record> myreviews = Db.paginate(page, 55, select,sqlExceptSelect,ucode);
			setAttr("myreviewPage",myreviews);
			String sql_ = "select count(*) ddfk from tblporder tblp where tblp.odeal=1 and odeal1=0  and ucode=?";
			Record ddfkRc = Db.findFirst(sql_,ucode);
			setAttr("ddfk", ddfkRc.get("ddfk"));
			sql_ = "select count(*) ddsh from tblporder tblp where tblp.odeal3=1 and odeal4=0  and ucode=?";
			Record ddshRc = Db.findFirst(sql_,ucode);
			setAttr("ddsh", ddshRc.get("ddsh"));
			List<Record> reviewList = myreviews.getList();
			TreeMap<String,List<Record>> reviewMap = new TreeMap<String,List<Record>>();
			for(Record review : reviewList){
				String ocode = review.getStr("ocode");
				List<Record> temp = reviewMap.get(ocode);
				if(temp == null){
					temp = new ArrayList<Record>();
					temp.add(review);
					reviewMap.put(ocode, temp);
				}else{
					temp.add(review);
				}
			}
			
			setAttr("myreviews", reviewMap.descendingMap());
			render(WEB_INF_URL+"front/peo-order.jsp");
		}
		
		//加载等待付款的订单列表
		@Before(AuthInterceptor.class)
		public void ddfk(){
			String ucode = ((UserBean)getSessionAttr("user")).getStr("ucode");
			String select = "select tblp.phyprice,tblop.psum,tblpo.ocode,tblpo.otime,tblpo.oprice,tblpo.odeal,tblpo.odeal1,tblpo.odeal2,tblpo.odeal3,tblpo.odeal4,tblp.pname,tblp.pphoto,tblp.pcode,tbladdr.name,tblzf.zname";
			String sqlExceptSelect = "from tblshrmess tblsm inner join tblporder tblpo on tblsm.ocode= tblpo.ocode and tblsm.ucode=? and tblpo.odeal='1' and tblpo.odeal1='0' and tblpo.ostate=1 "+
			                         "inner join tblzhifu tblzf on tblzf.zcode=tblsm.zffs "+
					                 "inner join tbladdress tbladdr on tbladdr.adcode=tblsm.adcode "+
			                         "inner join tblorderproduct tblop on tblop.ocode=tblpo.ocode "+
			                         "inner join tblproduct tblp on tblp.pcode=tblop.pcode order by tblpo.ocode,tblpo.otime desc";
			Integer page = getParaToInt("page");
			if(page == null || "".equals(page)){
				page= 1;
			}
			Page<Record> myreviews = Db.paginate(page, 55, select,sqlExceptSelect,ucode);
			setAttr("myreviewPage",myreviews);
			String sql_ = "select count(*) ddfk from tblporder tblp where tblp.odeal=1 and odeal1=0 and ucode=?";
			Record ddfkRc = Db.findFirst(sql_,ucode);
			setAttr("ddfk", ddfkRc.get("ddfk"));
			sql_ = "select count(*) ddsh from tblporder tblp where tblp.odeal3=1 and odeal4=0 and ucode=?";
			Record ddshRc = Db.findFirst(sql_,ucode);
			setAttr("ddsh", ddshRc.get("ddsh"));
			List<Record> reviewList = myreviews.getList();
			TreeMap<String,List<Record>> reviewMap = new TreeMap<String,List<Record>>();
			for(Record review : reviewList){
				String ocode = review.getStr("ocode");
				List<Record> temp = reviewMap.get(ocode);
				if(temp == null){
					temp = new ArrayList<Record>();
					temp.add(review);
					reviewMap.put(ocode, temp);
				}else{
					temp.add(review);
				}
			}
			
			setAttr("myreviews", reviewMap.descendingMap());
			render(WEB_INF_URL+"front/peo-order.jsp");
		}
		
		//加载我的订单列表
		@Before(AuthInterceptor.class)
		public void myorder(){
			String ucode = ((UserBean)getSessionAttr("user")).getStr("ucode");
			String select = "select tblp.phyprice,tblop.psum,tblpo.ocode,tblpo.otime,tblpo.oprice,tblpo.odeal,tblpo.odeal1,tblpo.odeal2,tblpo.odeal3,tblpo.odeal4,tblp.pname,tblp.pphoto,tblp.pcode,tbladdr.name,tblzf.zname";
			String sqlExceptSelect = "from tblshrmess tblsm inner join tblporder tblpo on tblsm.ocode= tblpo.ocode and tblsm.ucode=? and tblpo.ostate=1 "+
			                         "inner join tblzhifu tblzf on tblzf.zcode=tblsm.zffs "+
					                 "inner join tbladdress tbladdr on tbladdr.adcode=tblsm.adcode "+
			                         "inner join tblorderproduct tblop on tblop.ocode=tblpo.ocode "+
			                         "inner join tblproduct tblp on tblp.pcode=tblop.pcode order by tblpo.ocode,tblpo.otime desc";
			Integer page = getParaToInt("page");
			if(page == null || "".equals(page)){
				page= 1;
			}
			Page<Record> myreviews = Db.paginate(page, 55, select,sqlExceptSelect,ucode);
			setAttr("myreviewPage",myreviews);
			String sql_ = "select count(*) ddfk from tblporder tblp where tblp.odeal=1 and odeal1=0 and ucode=?";
			Record ddfkRc = Db.findFirst(sql_,ucode);
			// 待付款订单 数量
			setAttr("ddfk", ddfkRc.get("ddfk"));
			sql_ = "select count(*) ddsh from tblporder tblp where tblp.odeal3=1 and odeal4=0  and ucode=?";
			Record ddshRc = Db.findFirst(sql_,ucode);
			// 待确认收获 数量
			setAttr("ddsh", ddshRc.get("ddsh"));
			List<Record> reviewList = myreviews.getList();
			TreeMap<String,List<Record>> reviewMap = new TreeMap<String,List<Record>>();
			for(Record review : reviewList){
				String ocode = review.getStr("ocode");
				List<Record> temp = reviewMap.get(ocode);
				if(temp == null){
					temp = new ArrayList<Record>();
					temp.add(review);
					reviewMap.put(ocode, temp);
				}else{
					temp.add(review);
				}
			}
			
			setAttr("myreviews", reviewMap.descendingMap());
			render(WEB_INF_URL+"front/peo-order.jsp");
		}
	
	
	/**
	 * 等待付款 --
	 */
	public void apply(){
		String ocode = getPara("ocode");
		String sql_ = StringUtils.getSql("sal.find.findOrderDetailByOcode", ocode);
		List<Record> recordList = Db.find(sql_);
		setAttr("recordList",recordList);
		render(WEB_INF_URL+"front/peo-order-apply.jsp");
	}
	
	/**
	 * 取消订单
	 * */
	public void cancelOrder(){
		String ocode = getPara("ocode");
		boolean bool = POrderBean.poDao.findById(ocode).set("odeal", "0").update();
		if(bool){
			myorder();
		}else{
			
		}
	}
	
	/**
	 * 用户假删除订单
	 * */
	public void delOrder(){
		String ocode = getPara("ocode");
		boolean bool = POrderBean.poDao.findById(ocode).set("ostate", 0).update();
		if(bool){
			myorder();
		}else{
			
		}
	}
	
	/**
	 * 查看已经完成的订单
	 * */
	public void showFinishOrder(){
		String ocode = getPara("ocode");
		String sql_ = StringUtils.getSql("sal.find.findOrderDetailByOcode", ocode);
		List<Record> recordList = Db.find(sql_);
		setAttr("record",recordList);
		render(WEB_INF_URL+"front/peo-order-finish.jsp");
	}
	
	/**
	 * 查看取消的订单
	 * */
	public void showCancelOrder(){
		String ocode = getPara("ocode");
		String sql_ = StringUtils.getSql("sal.find.findOrderDetailByOcode", ocode);
		List<Record> recordList = Db.find(sql_);
		setAttr("record",recordList);
		render(WEB_INF_URL+"front/peo-order-cancel.jsp");
	}
	
	/**
	 * 用户确认收货
	 * */
	public void commitShip(){
		String ocode = getPara("ocode");
		boolean bool = POrderBean.poDao.findById(ocode)
				       .set("odeal4", "1")
				       .set("time4", StringUtils.getFormatTime(new Date()))
				       .update();
		if(bool){
			myorder();
		}else{
			
		}
	}
	
	/**
	 * 已完成 --
	 */
	public void finish(){
		render(WEB_INF_URL+"front/peo-order-finish.jsp");
	}
	
	/**
	 * 已取消 --
	 */
	public void cancel(){
		render(WEB_INF_URL+"front/peo-order-cancel.jsp");
	}
	
	/**
	 * 等待发货 --
	 */
	public void undeliver(){
		String ocode = getPara("ocode");
		String sql_ = StringUtils.getSql("sal.find.findOrderDetailByOcode", ocode);
		List<Record> recordList = Db.find(sql_);
		setAttr("record",recordList);
		render(WEB_INF_URL+"front/peo-order-deliver-no.jsp");
	}
	
	/**
	 * 已发货 --
	 */
	public void delivered(){
		String ocode = getPara("ocode");
		String sql_ = StringUtils.getSql("sal.find.findOrderDetailByOcode", ocode);
		List<Record> recordList = Db.find(sql_);
		setAttr("record",recordList);
		render(WEB_INF_URL+"front/peo-order-deliver-yes.jsp");
	}
	
	/**
	 * 删除地址
	 * */
	public void delAddress(){
		String adcode = getPara("adcode");
		boolean bool = AddressBean.addressDao.findById(adcode).set("adstate", 0).update();
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 订单中新增地址
	 * */
	public void addNewAddress(){
		String ucode = ((UserBean)getSessionAttr("user")).getStr("ucode");
		String shrname = getPara("shrname");
		String addr = getPara("addr");
		String tel = getPara("tel");
		String gdtel = getPara("gdtel");
		String email = getPara("email");
		String youbian = getPara("youbian");
		AddressBean address = new AddressBean();
		boolean bool = address
				       .set("adcode", StringUtils.getTimeCode())
				       .set("ucode", ucode)
				       .set("name", shrname)
				       .set("addr", addr)
				       .set("tel", tel)
				       .set("gdtel", gdtel)
				       .set("email", email)
				       .set("youbian", youbian)
				       .set("adstate", 1)  // 1:可用   0：不可用
				       .save();
		if(bool){
			renderJson("message",address.get("adcode"));
		}else{
			renderJson("message","false");
		}		             
	}
}
